package com.rkjoeson.pulltorefresh;

import androidx.annotation.NonNull;

import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.header.TwoLevelHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

public class PullToRefreshViewGroup extends ViewGroupManager<SmartRefreshLayout> {

    private SmartRefreshLayout smartRefreshLayout;
    private ThemedReactContext context;


    private final String CLASSICS = "classics";
    private final String DADAR = "radar";
    private final String MATERIAL = "material";
    private final String TWOLEVEL = "top-level";
    private final String BALL = "ball";

    @NonNull
    @Override
    public String getName() {
        return "RCTPullToRefresh";
    }

    @NonNull
    @Override
    protected SmartRefreshLayout createViewInstance(@NonNull ThemedReactContext reactContext) {
        context = reactContext;
        smartRefreshLayout = new SmartRefreshLayout(reactContext);
        smartRefreshLayout.setRefreshFooter(new ClassicsFooter(context));
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
        return smartRefreshLayout;
    }

    /**
     * 配置安卓下啦刷新的风格
     *
     * @param smartRefreshLayout SmartRefreshLayout
     * @param style              string
     */
    @ReactProp(name = "header")
    public void setHeaderStyle(SmartRefreshLayout smartRefreshLayout, @NonNull String style) {
        switch (style) {
            case CLASSICS:
                smartRefreshLayout.setRefreshHeader(new ClassicsHeader(context));
                break;

            case DADAR:
                smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(context));
                break;

            case MATERIAL:
                smartRefreshLayout.setRefreshHeader(new MaterialHeader(context));
                break;

            case TWOLEVEL:
                smartRefreshLayout.setRefreshHeader(new TwoLevelHeader(context));
                break;

        }
    }

    /**
     * @param smartRefreshLayout
     * @param style
     */
    @ReactProp(name = "disabledRefresh")
    public void setRefreshDisabledStatus(SmartRefreshLayout smartRefreshLayout, int style) {

    }
}
